package com.lanxiao.cloud.modules.dept.application;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import com.lanxiao.cloud.common.core.enums.DelFlagEnum;
import com.lanxiao.cloud.common.web.service.ILog;
import com.lanxiao.cloud.modules.dept.application.assembler.DeptAssembler;
import com.lanxiao.cloud.modules.dept.application.dto.BasicsDeptTreeDto;
import com.lanxiao.cloud.modules.dept.application.dto.DeptTreeDto;
import com.lanxiao.cloud.modules.dept.domain.model.Dept;
import com.lanxiao.cloud.modules.dept.infrastructure.repository.DeptMapper;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2021-10-26
 */
@Component
public class DeptQueryManager implements ILog<Dept, Long> {

  private final DeptMapper deptMapper;
  private final DeptAssembler deptAssembler;

  public DeptQueryManager(
      DeptMapper deptMapper,
      DeptAssembler deptAssembler) {
    this.deptMapper = deptMapper;
    this.deptAssembler = deptAssembler;
  }

  /**
   *  获取树形部门列表
   *
   * @author ：qiaoliang

   * @return java.util.List<com.lanxiao.cloud.modules.dept.application.dto.DeptTreeDto>
   * @date 2021-10-26 11:32
   */
  public List<DeptTreeDto> all() {
    return deptAssembler.toDeptTreeDto(deptMapper.selectList(
        new LambdaQueryWrapper<Dept>().eq(Dept::getDelFlag, DelFlagEnum.NOT_DELETE)));
  }

  public List<BasicsDeptTreeDto> query() {
    return deptAssembler.toDeptUserTreeDto(deptMapper.selectList(
        new LambdaQueryWrapper<Dept>().eq(Dept::getDelFlag, DelFlagEnum.NOT_DELETE)));
  }

  /**
   *  批量获取部门信息（回填部门时使用）
   * 返回：部门>部门>部门
   * @author ：qiaoliang
   * @param ids : 部门ID
   * @return java.util.Map<java.lang.Long, java.lang.String>
   * @date 2021-10-26 11:42
   */
  public Map<Long, String> findIds(List<Long> ids) {

    final List<Dept> deptList = deptMapper.selectList(
        new LambdaQueryWrapper<Dept>()
            .ne(Dept::getId, 1L)
            .eq(Dept::getDelFlag, DelFlagEnum.NOT_DELETE));

    if (CollUtil.isEmpty(deptList)) {
      return Maps.newHashMap();
    }

    final Map<Long, String> deptNames = deptList.stream()
        .collect(Collectors.toMap(Dept::getId, Dept::getName));

    Map<Long, String> map = new HashMap<>(ids.size());

    deptList.forEach(dept -> {
      if (ids.contains(dept.getId())) {
        final List<Long> parentIds = JSONUtil.toList(JSONUtil.parseArray(dept.getParentIds()),
            Long.class);
        parentIds.add(dept.getId());
        map.put(dept.getId(), parentIds.stream().map(deptNames::get)
            .filter(Objects::nonNull).reduce((v, i) -> v+">"+i).orElse(""));
      }
    });
    return map;
  }

  @Override
  public Dept selectById(Long id) {
    return deptMapper.selectById(id);
  }

}
